package com.baosight.sczx.zm.cl.service;

import com.baosight.iplat4j.core.ei.EiConstant;
import com.baosight.iplat4j.core.ei.EiInfo;
import com.baosight.iplat4j.core.threadlocal.UserSession;
import com.baosight.iplat4j.ep.ServiceEPBase;
import com.baosight.sczx.common.zm.basetools.StaticTools;
import com.baosight.sczx.common.zm.basetools.TablePk;
import com.baosight.sczx.common.zm.cInterface.Department;
import com.baosight.sczx.common.zm.cInterface.User;
import com.baosight.sczx.common.zm.cInterface.cc.Qualitystandard;
import com.baosight.sczx.common.zm.cInterface.cc._5ExamineClassify;
import com.baosight.sczx.common.zm.cInterface.cf.MessageUtil;
import com.baosight.sczx.common.zm.cInterface.cf.Penalty;
import com.baosight.sczx.common.zm.cInterface.cl.OperationHistory;
import com.baosight.sczx.common.zm.cInterface.cl.RoundCheckWorkNote;
import com.baosight.sczx.common.zm.cInterface.cs.InitialData;
import com.baosight.sczx.common.zm.cInterface.cs.WorkSchedule;
import com.baosight.sczx.zm.cl.domain.ZMCLTernateRecord;

/**
 * 三违处理模块
 * 待处理三违记录
 * <p>Title: ServiceZMCL61.java</p>
 * <p>Description: 三违处理：待处理的服务类</p>
 * <p>Copyright: Baosight Software LTD.co Copyright (c) 2002</p>
 * <p>Company: Baosight Software</p>
 * @author 高强
 * @version 1.0
 * <pre>Histroy:
 *          2011-6-24 上午 高强  Create
 *</pre>
 */
public class ServiceZMCL61 extends ServiceEPBase {
	
	/** 
	 * <p>Function: initLoad</p>
	 * <p>Description: 当前页面托管加载初始化操作</p>
	 * <p>return value: EiInfo</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-6-24 上午 高强
	 * </p>
	 *
	*/
	@Override
	public EiInfo initLoad(EiInfo inInfo) 
	{
		inInfo.addBlock(EiConstant.resultBlock).set("showCount", "true");//如果不加这个，翻页的统计和显示是错误的
		EiInfo outInfo =  this.query(inInfo);
		outInfo.addBlock(InitialData.getInstance().getEiBlockByNo("cmes.cl.classify"));//三违分类
		outInfo.addBlock(InitialData.getInstance().getEiBlockByNo("cmes.cl.sourceClassify"));//三违来源
		outInfo.addBlock(WorkSchedule.getWorkScheduleBlock("cmes.cl.workNum", false));//班次
		outInfo.addBlock(Department.getInstance().getConstructionUnit("cmes.cl.department", "orgCode", "orgName", false));//三违单位
		outInfo.addBlock(User.getInstance().getConstructionStaff("cmes.cl.responsibilityPeople", "empCode", "empName", false));//检查人
		//outInfo.addBlock(WorkingFace.getAllWorkingFace("cmes.cl.workFace", true));//所在工作面
		//outInfo.addBlock(RoadWay.getAllRoadWay("cmes.cl.roadWay", true));//所在巷道
		outInfo.addBlock(RoundCheckWorkNote.getAllNoteCode("cmes.cl.workNote", true));//巡检工单
		outInfo.addBlock(InitialData.getInstance().getEiBlockByNo("cmes.cl.penaltyType"));//罚款分类
		outInfo.addBlock(Qualitystandard.getQualityStandardItems("cmes.cl.quality", "在用", true));//质量标准化分类
		outInfo.addBlock(_5ExamineClassify.get5examineClassify("cmes.cl.examine", "在用", true));//五个到位分类
		
		outInfo.set("departmentID", UserSession.getInSessionProperty("orgCode"));//跳转罚款页面开单单位的默认值
		outInfo.set("departmentName", UserSession.getInSessionProperty("orgName"));
		return outInfo;
	}
	
	/**
	 * <p>Function: query</p>
	 * <p>Description:查询未处理三违记录 </p>
	 * <p>return value: EiInfo</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-6-24 上午 高强
	 * </p>
	 *
	*/
	@Override
	public EiInfo query(EiInfo inInfo) {
		//StaticTools.setQueryParameters(inInfo, "fdManageDepartmentId",UserSession.getInSessionProperty("orgCode").toString(), true);//查询所有安全管理单位下录入的三违记录
		StaticTools.setQueryParameters(inInfo, "isHandling", "yes", true);
		EiInfo outInfo = super.query(inInfo, "ZMCLTernateRecord.query", new ZMCLTernateRecord());
		return outInfo;
	}
	
	/**
	 * <p>Function: insert</p>
	 * <p>Description: 新增三违</p>
	 * <p>return value:EiInfo</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-6-24   高强  
	 * </p>
	 *
	 */
	public EiInfo insert(EiInfo inInfo)
	{
		String fdKeyId = TablePk.getInstance().getNextValue("T_CL_TERNATE_RECORD");
		String fdCheckPeopleId = inInfo.getBlock("result").getCellStr(0, "fdCheckPeopleId");//检查人ID
		
        //在"三违记录表"中新增一条记录
		inInfo.getBlock("result").setCell(0, "fdKeyId", fdKeyId);
		inInfo.getBlock("result").setCell(0, "fdStatus", "未处理");
		inInfo.getBlock("result").setCell(0, "fdCheckPeople", fdCheckPeopleId);
		inInfo.getBlock("result").setCell(0, "fdIsPenalty", "否");
		inInfo.getBlock("result").setCell(0, "fdCreateTime", StaticTools.getDateTime());
		inInfo.getBlock("result").setCell(0, "fdCreateUserId", UserSession.getUserId());//录入人ID
		inInfo.getBlock("result").setCell(0, "fdCreateUser", UserSession.getInSessionProperty("UserCname"));//录入人
		
		EiInfo outInfo = super.insert(inInfo, "ZMCLTernateRecord.insert");
		boolean insertValue = OperationHistory.InsertOperationHistory(inInfo, "三违","新增");//新增操作记录
		if(!insertValue) {
			outInfo.setStatus(-1);
			outInfo.setMsg("插入操作记录失败！");
			return outInfo;
		}
		return outInfo;
	}
	
	/**
	 * 修改三违记录
	 * <p>Function: update</p>
	 * <p>Description: 处理未处理的三违记录</p>
	 * <p>return value: EiInfo</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *   2011-6-24 上午 高强
	 * </p>
	 *
	*/
	@Override
	public EiInfo update(EiInfo inInfo) {
		String fdCheckPeopleId = inInfo.getBlock("result").getCellStr(0, "fdCheckPeopleId");//检查人ID
		
		inInfo.getBlock("result").setCell(0, "fdCheckPeople", fdCheckPeopleId);
		inInfo.getBlock("result").setCell(0, "fdCreateTime", StaticTools.getDateTime());
		inInfo.getBlock("result").setCell(0, "fdCreateUserId", UserSession.getUserId());//录入人ID
		inInfo.getBlock("result").setCell(0, "fdCreateUser", UserSession.getInSessionProperty("UserCname"));//录入人
		EiInfo outInfo = super.update(inInfo, "ZMCLTernateRecord.update");
		boolean insertValue = OperationHistory.InsertOperationHistory(inInfo, "三违","修改");//新增操作记录
		if(!insertValue) {
			outInfo.setStatus(-1);
			outInfo.setMsg("插入操作记录失败！");
			return outInfo;
		}
		return outInfo;
	}
	
	/**
	 * 三违处理
	 * 下达三违通知
	 * <p>Function: modifyStop</p>
	 * <p>Description: </p>
	 * <p>return value:EiInfo</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-4-18 上午 高强
	 *    2011-8-19 modified by wangkun 增加发送消息部分
	 * </p>
	 *
	 */
	public EiInfo handle(EiInfo inInfo)
	{
		super.update(inInfo, "ZMCLTernateRecord.updateForHandle");//更新三违记录数据
		
		String fdIsPenalty = inInfo.getBlock("result").getCellStr(0, "fdIsPenalty");//是否罚款
		String fdTernateRecordId = inInfo.getBlock("result").getCellStr(0, "fdKeyId");//三违记录ID
		String fdTernateHandleId = TablePk.getInstance().getNextValue("T_CL_TERNATE_HANDLE");//三违处理ID
		
		inInfo.getBlock("result").setCell(0, "fdTernateRecordId", fdTernateRecordId);//三违记录ID
		inInfo.getBlock("result").setCell(0, "fdIsPenalty", "否");//无论选择是否罚款,在没有提交罚款前就是未罚款
		inInfo.getBlock("result").setCell(0, "fdPenalty", "0");//罚款金额初始为0
		inInfo.getBlock("result").setCell(0, "fdKeyId", fdTernateHandleId);
		inInfo.getBlock("result").setCell(0, "fdCreateTime", StaticTools.getDateTime());
		
		EiInfo outInfo = super.insert(inInfo, "ZMCLTernateHandle.insert");//新增三违处理记录数据
		
		if("是".equals(fdIsPenalty)) {
			String orgCode = UserSession.getInSessionProperty("orgCode") == null ? "" : UserSession.getInSessionProperty("orgCode").toString();
			outInfo.set("fdManageDepartment", Department.getInstance().getDepartmentNameById(orgCode));
			outInfo.set("fdTernateRecordId", fdTernateRecordId);
			outInfo.addBlock(InitialData.getInstance().getEiBlockByNo("cmes.cl.penaltyType"));//罚款分类
		}

		//发消息
		if(outInfo.getStatus() != -1)
		{
			String departmentId = inInfo.getBlock(EiConstant.resultBlock).getCellStr(0, "fdResponseDepartmentId");
			String people = inInfo.getBlock(EiConstant.resultBlock).getCellStr(0, "fdResponsibilityPeople");
			
			if(people.length()>60)
			{
				people = people.substring(0, 60) + "...";
			}
			
			MessageUtil messageUtil = new MessageUtil();
			messageUtil.setDao(dao);
			int result = messageUtil.insertIntoMessageCache("下达关于责任人"+people+"的三违通知", "ZMCL80", departmentId, "", 1);
			
			if(result != 1)
			{
				System.out.println("消息发送失败："+result);
			}
		}
		//end
		return outInfo;
	}	
	
	/**
	 * 罚款
	 * <p>Function: penalty</p>
	 * <p>Description: </p>
	 * <p>return value:EiInfo</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-8-7下午02:36:58 严洪浩
	 *    2011-8-19 modified by wangkun  增加发送消息
	 * </p>
	 *
	 */
	public EiInfo penalty(EiInfo inInfo) {						
		String fdKeyId = TablePk.getInstance().getNextValue("T_CF_PENALTY");//罚款单ID
		boolean value = Penalty.Insert(inInfo, fdKeyId);//罚款
		
		if(value)
		{
			//更新三违处理记录 直接罚款的不需求更新
			String fdTernateRecordId = inInfo.getBlock("result").getCellStr(0, "fdTernateRecordId");//三违记录ID
			String fdTernateHandleId = inInfo.getBlock("result").getCellStr(0, "fdTernateHandleId");//三违处理ID
			
			if(null != fdTernateHandleId && !"".equals(fdTernateHandleId.trim()))
			{
				inInfo.getBlock("result").setCell(0, "fdPenaltyId", fdKeyId);
				super.update(inInfo, "ZMCLTernateHandle.modifyPenalty");
			}
			
			StaticTools.setQueryParameters(inInfo, "fdKeyId", fdTernateRecordId, true);//三违记录ID
			EiInfo _tempInfo = super.query(inInfo, "ZMCLTernateRecord.query", new ZMCLTernateRecord());
			
			String fdIsPenalty = _tempInfo.getBlock("result").getCellStr(0, "fdIsPenalty");//是否罚款
			String fdPenaltyIds = _tempInfo.getBlock("result").getCellStr(0, "fdPenaltyIds");//罚款记录ID'S
			

			if("否".equals(fdIsPenalty))
			{
				inInfo.getBlock("result").setCell(0, "fdPenaltyIds", fdKeyId);
				inInfo.getBlock("result").setCell(0, "fdIsPenalty", "是(1)");
			}
			else
			{
				String _number = fdIsPenalty.toString().substring(fdIsPenalty.toString().indexOf("(")+1, fdIsPenalty.toString().indexOf(")"));
				
				int number = 2;//默认为2
				try {
					number = Integer.parseInt(_number) + 1;
				} catch (NumberFormatException e) {
					System.out.println("三违罚款时数据转换错误 : " + e.getMessage());
				}
				
				inInfo.setCell("result", 0, "fdIsPenalty", "是("+number+")");
				inInfo.setCell("result", 0, "fdPenaltyIds", fdPenaltyIds+","+fdKeyId);
			}
			
			//更新三违记录
			EiInfo outInfo = super.update(inInfo, "ZMCLTernateRecord.modifyPenalty");
			return outInfo;
		}
		else
		{
			inInfo.setStatus(-1);
			inInfo.setMsg("罚款新增失败！");
			return inInfo;
		}				
	}
	
	/**
	 * <p>Function: modifyStop</p>
	 * <p>Description: 关闭</p>
	 * <p>return value:EiInfo</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-4-18 上午 高强
	 * </p>
	 *
	 */
	public EiInfo close(EiInfo inInfo)
	{
		EiInfo _temp = super.update(inInfo, "ZMCLTernateRecord.modifyStop");
		boolean insertValue = OperationHistory.InsertOperationHistory(inInfo, "三违","关闭");//新增操作记录
		if(!insertValue) {
			_temp.setStatus(-1);
			_temp.setMsg("插入操作记录失败！");
			return _temp;
		}
        EiInfo outInfo = this.query(inInfo);
        outInfo.setMsg(_temp.getMsg());
        outInfo.setDetailMsg(_temp.getDetailMsg());
		return outInfo;
	}	

	/**
	 * 作废
	 * <p>Function: invalid</p>
	 * <p>Description: </p>
	 * <p>return value:EiInfo</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-8-8下午02:33:27 严洪浩
	 * </p>
	 *
	 */
	public EiInfo invalid(EiInfo inInfo) {
		EiInfo _temp = super.update(inInfo, "ZMCLTernateRecord.modifyInvalid");
		boolean insertValue = OperationHistory.InsertOperationHistory(inInfo, "三违","作废");//新增操作记录
		if(!insertValue) {
			_temp.setStatus(-1);
			_temp.setMsg("插入操作记录失败！");
			return _temp;
		}
        EiInfo outInfo = this.query(inInfo);
        outInfo.setMsg(_temp.getMsg());
        outInfo.setDetailMsg(_temp.getDetailMsg());
		return outInfo;
	}
}
